package com.flynn.rock.handler;

import com.flynn.rock.model.Response;
import com.flynn.rock.util.HttpServletUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class SysLoginFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
                                        AuthenticationException exception) throws IOException, ServletException {
        HttpServletUtil.response(response, Response.fail("登录失败[" + exception.getMessage() + "]"), HttpStatus.NETWORK_AUTHENTICATION_REQUIRED);
    }
}
